package org.arl.modem.transportARQ;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;

class DrawTimeline2 {
   public static void main(String args[]) {
      // List<Long> SenderTx = new LinkedList<Long>();
      // List<Long> SenderRx = new LinkedList<Long>();
      try {
         FileInputStream fstream = new FileInputStream(
         // "/home/rohit/Desktop/arl/mod1/modem-sim/logs/log-0.txt");
               "/home/rohit/Desktop/pan3/tx11.txt");
         DataInputStream in = new DataInputStream(fstream);
         BufferedReader br = new BufferedReader(new InputStreamReader(in));
         FileWriter outFilestx = new FileWriter(
"/home/rohit/Desktop/pan3a/tx11a");
         PrintWriter outstx = new PrintWriter(outFilestx);
         // FileWriter outFilesrx = new
         // FileWriter("/home/rohit/Desktop/senderrx");
         // PrintWriter outsrx = new PrintWriter(outFilesrx);
         long l1 = 0;
         long prevl1 = 0;
         String strLine;
         long first = 0;
         int txc1 = 1;
         int rxc1 = 1;
         while ((strLine = br.readLine()) != null) {
            // Print the content on the console
            if (strLine.contains("~PTX.PKT.TIME=")) {
               String s = strLine.split("~PTX.PKT.TIME=")[1].split(",")[0];
               l1 = Long.parseLong(s) / 1000;
               if (first == 0)
                  first = l1;
               if (prevl1 == 0)
                  prevl1 = l1;
               System.out.println(l1);
               long diff = l1 - first;
               long gap = l1 - prevl1;
               prevl1 = l1;
               outstx.println(0 + "," + txc1 + "," + s + "," + diff + "," + gap);
               txc1++;
            } else if (strLine.contains("~PRX.PKT.TIME=")) {
               String s = strLine.split("~PRX.PKT.TIME=")[1].split(",")[0];
               l1 = Long.parseLong(s) / 1000;
               if (first == 0)
                  first = l1;
               if (prevl1 == 0)
                  prevl1 = l1;
               System.out.println(l1);
               long diff = l1 - first;
               long gap = l1 - prevl1;
               prevl1 = l1;
               outstx.println(1 + "," + rxc1 + "," + s + "," + diff + "," + gap);
               rxc1++;
               // outstx.println(s);
            }
         }
         outstx.close();
         // outsrx.close();
         in.close();

         FileInputStream fstream2 = new FileInputStream(
         // "/home/rohit/Desktop/arl/mod2/modem-sim/logs/log-0.txt");
               "/home/rohit/Desktop/pan3/rx11.txt");
         DataInputStream in2 = new DataInputStream(fstream2);
         BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
         FileWriter outFilertx = new FileWriter(
"/home/rohit/Desktop/pan3a/rx11a");
         PrintWriter outrtx = new PrintWriter(outFilertx);
         // FileWriter outFilerrx = new
         // FileWriter("/home/rohit/Desktop/recvrx");
         // PrintWriter outrrx = new PrintWriter(outFilerrx);
         l1 = 0;
         prevl1 = 0;
         // String strLine;
         first = 0;
         String strLine2;
         int txc2 = 1;
         int rxc2 = 1;
         while ((strLine2 = br2.readLine()) != null) {

            if (strLine2.contains("~PTX.PKT.TIME=")) {
               String s = strLine2.split("~PTX.PKT.TIME=")[1].split(",")[0];
               l1 = Long.parseLong(s) / 1000;
               if (first == 0)
                  first = l1;
               if (prevl1 == 0)
                  prevl1 = l1;
               System.out.println(l1);
               long diff = l1 - first;
               long gap = l1 - prevl1;
               prevl1 = l1;
               outrtx.println(0 + "," + txc2 + "," + s + "," + diff + "," + gap);
               txc2++;
               // outrtx.println(s);
            } else if (strLine2.contains("~PRX.PKT.TIME=")) {
               String s = strLine2.split("~PRX.PKT.TIME=")[1].split(",")[0];
               l1 = Long.parseLong(s) / 1000;
               if (first == 0)
                  first = l1;
               if (prevl1 == 0)
                  prevl1 = l1;
               System.out.println(l1);
               long diff = l1 - first;
               long gap = l1 - prevl1;
               prevl1 = l1;
               outrtx.println(1 + "," + rxc2 + "," + s + "," + diff + "," + gap);
               rxc2++;
               // outrtx.println(s);
            }
         }
         // outrrx.close();
         outrtx.close();
         in2.close();
      } catch (Exception e) {// Catch exception if any
         System.err.println("Error: " + e.getMessage());
      }
   }
}
